﻿@{
    
    Layout = null;
}

<h2>GiveEmpAllotDept</h2>
<link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/js/jquery.min.js"></script>
<script src="~/Scripts/js/bootstrap.min.js"></script>
<input id="txtEmpID" type="text" placeholder="请输入员工编号" />
<input type="button" value="搜索" onclick="LoadEmpList(1)" />
<table id="t1" class="table-bordered table table-condensed table-responsive table-hover">
    <thead>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>部门</th>
            <th>职位</th>
            <th>技术特长</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>
<div class="modal fade" data-keyboard="false" data-show="false" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true" id="cha">
                    ×
                </button>
            </div>
            <div class="modal-body text-center" style="height:auto;margin:0 auto">
                <input type="hidden" id="EmpId" />
                <input type="button" value="部门调用" class="btn bg-primary" style="width:150px;height:50px;" onclick="AllotDept()" />
                <input type="button" value="职位变更" class="btn bg-primary" style="width:150px;height:50px;" onclick="AllotPosition()" /><br />
                <p style="padding-top:20px;display:none;"><span>部门名称:</span><select id="Select1"></select></p>
                <p style="padding-top:20px;display:none;"><span>职位名称:</span><select id="Select2"></select></p>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button><button type="button" class="btn btn-primary" onclick="SaveOperate()">
                    提交更改
                </button>

            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<footer></footer>
<script>

    $(function () {
        LoadEmpList(1);
        //DeptList();
        //PositionList();
    })

    var PageSize = 10;

    //加载员工信息
    function LoadEmpList(PageIndex) {
        var EmployeeID = $("#txtEmpID").val();
        if (EmployeeID == "") {
            EmployeeID = "0";
        }
        $.ajax({
            type: "Post",
            url: "/Employee/EmployeeInfoList",
            data: { PageIndex: PageIndex, PageSize: PageSize, EmployeeID: EmployeeID },
            success: function (obj) {
                obj = JSON.parse(obj);
                //alert(obj.list);
                var str = "";
                $("#t1 tbody").empty();
                for (var i = 0; i < obj.list.length; i++) {
                    str += "<tr><td>" + obj.list[i].EmployID + "</td><td>" + obj.list[i].EmployName + "</td><td>" + obj.list[i].Age + "</td><td>" + obj.list[i].DeptName + "</td><td>" + obj.list[i].PositionName + "</td><td>" + obj.list[i].Specialty + "</td><td><a href='#' data-toggle='modal' data-target='#myModal' onclick='SaveEmpID(" + obj.list[i].EmployID + ")'>分配</a></td></tr>"
                }
                $("#t1 tbody").html(str);
                Pager(obj.Count);
            }
        })
    }

    //分页
    function Pager(count) {
        var pages = Math.ceil(count / PageSize);
        $("footer").empty();
        for (var i = 1; i <= pages; i++) {
            $("footer").append("<a href='#' onclick='LoadEmpList(" + i + ")'>" + i + "</a>&nbsp;")
        }
    }
    //保存员工Id
    function SaveEmpID(EmpId) {
        $("#EmpId").val(EmpId);
    }

    //部门信息/下拉框
    function DeptList() {
        $.ajax({
            type: "post",
            url: "/Employee/RoleList",
            data: { DeptName: "" },
            success: function (obj) {
                obj = JSON.parse(obj);
                var str = "";
                for (var i = 0; i < obj.length; i++) {
                    str += "<option value='" + obj[i].DeptID + "'>" + obj[i].DeptName + "</option>"
                }
                $("#Select1").html("<option value='0'>请选择</option>" + str);
            }
        })
    }

    //职位信息/下拉框
    function PositionList() {
        $.ajax({
            type: "post",
            url: "/Employee/PositionList",
            success: function (obj) {
                obj = JSON.parse(obj);
                var str = "";
                for (var i = 0; i < obj.length; i++) {
                    str += "<option value='" + obj[i].PositionID + "'>" + obj[i].PositionName + "</option>"
                }
                $("#Select2").html("<option value='0'>请选择</option>" + str);
            }
        })
    }

    //部门调动
    function AllotDept() {
        //显示部门信息
        DeptList();
        $("#Select2").parent().css("display", "none");
        $("#Select1").parent().css("display", "block");
    }

    //职位变更
    function AllotPosition() {
        PositionList();
        $("#Select1").parent().css("display", "none");
        $("#Select2").parent().css("display", "block");
    }


    //保存修改
    function SaveOperate() {
        var EmpId = $("#EmpId").val();
        var DeptID = 0;
        var PositionID = 0;
        //判断当前是执行什么操作
        if ($("#Select1").val() != 0 && $("#Select1").val() != null) {
            DeptID = $("#Select1").val();
        }
        if ($("#Select2").val() != 0 && $("#Select2").val() != null) {
            PositionID = $("#Select2").val();
        }

        if (DeptID == 0 && PositionID == 0) {
            alert("请选择要执行的操作");
        }
        else {
            $.ajax({
                type: "post",
                url: "/Employee/SaveEmpDeptAllot",
                data: { EmpId: EmpId, DeptID: DeptID, PositionID: PositionID },
                success: function (obj) {
                    if (obj > 0) {
                        document.getElementById("cha").click();
                        $("#Select1,#Select2").val("0");
                        $("#Select1").parent().css("display", "none");
                        $("#Select2").parent().css("display", "none");
                        LoadEmpList(1);
                    }
                }
            })
        }

        //$("p").each(function () {
        // if ($(this).css("display") == "block") {
        //      alert($(this).children("select").val());
        //  }
        //})

    }
</script>